import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/LoginView.vue";
import AdminView from "@/views/AdminView.vue";
import store from "../store";
import ElementUI from "element-ui";
import Index from "@/views/Index.vue";
import userView from "@/views/UserView.vue";
Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/admin',
    name: 'admin',
    component: AdminView,
    children:[
        {
            path: "",
            name: 'index',
            component:Index
        },
      {
        path: "user",
        name: 'user',
        component:userView
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    if(!store.state.user){
      ElementUI.Message.error("请先登录")
      next({path: '/login'})
    }else {
        next()
    }
  }
})

export default router
